<?php
  /**
   * Example file
   * This file contains an exemplary use of the CICP PHP Framework.
   * The example shows how to use the class Cicp and how to write
   * a handler to manage graphs.
   *
   * Files:
   * - example.php - (this file) basic CICP app
   * - autoload.php - contains the autoload function for the Cicp
   * 	classes
   * - CicpHelloWorldHandler.php - a basic handler that creates graphs
   * 	that creates graphs with one node displaying "Hello, World!"
   * 	and has some user interaction
   *
   * License: GPLv3 and later, you may use, modify and distribute these files.
   */
ob_start();

file_put_contents('example.log', "\n<-\n" . print_r($_GET, true), FILE_APPEND);

error_reporting(E_ALL | E_STRICT);

  // set content-type to text
    header("Content-Type: text/plain");

// include needed files
require_once('autoload.php'); // autoload function for cicp classes
    
try {

    // set up new cicp processor
    // this is done by getInstance() to have only one instance of Cicp running
    $cicp = Cicp::getInstance();
    
    // set storage driver to the Filesystem Driver
    // The Filesystem Driver stores graphs on the harddisk with a
    // folder/files structure
    $cicp->setStorageDriver(new Cicp_StorageDriver_Filesystem);
    
    // set user handler
    if (isset($_GET['producer'])) {
        switch ($_GET['producer']) {
        case 'asciiimage':
            require_once('CicpAsciiImage.php');
            $cicp->setUserHandler(new CicpAsciiImage);
            break;
        case 'devtest':
            require_once('CicpDeveloperTest.php');
            $cicp->setUserHandler(new CicpDeveloperTest);
            break;
        case 'myownproducer':
            require_once('myProducer.php');
            $cicp->setUserHandler(new myProducer);
            break;
        case 'randompoints':
            require_once('CicpRandomPoints.php');
            $cicp->setUserHandler(new CicpRandomPoints);
            $cicp->getOutputProcessor()->addProcessor(new Cicp_IOProcessor_ExtendLines);
            break;
        case 'filesystem':
            require_once('CicpFileSystem.php');
            $cicp->setUserHandler(new CicpFileSystem);
            break;
        case 'helloworld':
            require_once('CicpHelloWorldHandler.php');
            $cicp->setUserHandler(new CicpHelloWorldHandler);
            break;
        case 'drawboard':
            require_once('CicpDrawBoard.php');
            $cicp->setUserHandler(new CicpDrawBoard);
            break;
        case 'agilezenprojects':
            require_once('CicpAgilezenProjects.php');
            $cicp->setUserHandler(new CicpAgilezenProjects);
            break;
        default:
            throw new Exception('error: unkown producer');
        }
    }
    else {
        throw new Exception('error: no producer given');
    }
    
    if (isset($_GET['msg'])) {
        // process input
        try {
            $cicp->processInput($_GET['msg']);
        }
        catch (Exception $e) {
            echo $e->getMessage();
        }
        echo $cicp->getOutput(); // getOutput() returns the generated CICP response
    }
    else {
        die("Welcome to PHP Hub!");
    }
}
catch (Exception $e) {
    echo "error: " + $e->getMessage();
}

// print debug information if parameter 'debug' is given
// this will print the whole cicp object to find errors quickly
if (isset($_GET['debug'])) {
    print_r($cicp);
 }

file_put_contents('example.log', "\n->\n" .  ob_get_contents() . "\n", FILE_APPEND);
